/************************* 版权声明 *********************************
 * 
 * 版权所有：Apache License 2.0
 * 
 ************************* 版本声明 *********************************
 *@since jdk1.5
 *@version 0.1
 ************************* 变更记录 *********************************
 *
 * 创建者：bijiqiang Email:bijiqiang@gmail.com   创建日期： 2010-1-5
 * 创建记录：创建类结构。
 * 
 * 修改者：       修改日期：
 * 修改记录：
 *
 * 修改者：       修改日期：
 * 修改记录：
 * ……
 ************************* To  Do *********************************
 *
 * 1、实现所有未实现的方法（抛出UnsupportedOperationException的方法）。
 * 
 ************************* 随   笔 *********************************
 *
 * 
 * 
 ******************************************************************
 */

package com.realgodo.site.service.site;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.site.Recruit;

@Service("recruitManager")
@Transactional
public class RecruitManager extends DefaultEntityManager<Recruit, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	public Page<Recruit> findEvents(int pageSize, int overFlag) {
		Page<Recruit> page = new Page<Recruit>(pageSize);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Recruit.class);
		if (overFlag != Recruit.OVER_ALL) {
			c.add(Restrictions.eq("over", overFlag));
		}
		c.addOrder(Order.desc("createDate"));
		return this.entityDao.findByCriteria(page, c);
	}

	public Recruit createRecruit(Recruit recruit) {
		return (Recruit) this.saveMerge(recruit);
	}

	public void removeRecruit(Long[] ids) {
		for (Long id : ids) {
			this.delete(id);
		}
	}

	public Recruit topRecruit(Long id) {
		Recruit recruit = this.get(id);
		recruit.setTop(Recruit.TOP_YES);
		return (Recruit) this.saveMerge(recruit);
	}

	public Recruit overRecruit(Long id) {
		Recruit recruit = this.get(id);
		recruit.setOver(Recruit.OVER_YES);
		return (Recruit) this.saveMerge(recruit);
	}

	public List<Recruit> findFontRecruits(int maxResult) {
		Page<Recruit> page = new Page<Recruit>(maxResult);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Recruit.class);
		c.add(Restrictions.eq("over", Recruit.OVER_NOT));
		c.addOrder(Order.desc("top"));
		c.addOrder(Order.desc("createDate"));
		return this.entityDao.findByCriteria(page, c).getResult();
	}
}
